package com.zlcdgroup.delegates

import java.math.BigDecimal
import java.text.DateFormat
import java.text.SimpleDateFormat

/**
 *参数委托校验类
 *@author pei
 *@date 2024/9/26 16:15
 *
 */
class DelegatesValidator {
    companion object {
        fun createBuilder(nullErrorMsg: String) = DelegatesValidatorBuilder(nullErrorMsg)
    }

    data class DelegatesValidatorBuilderParams(
        val nullErrorMsg: String,
        val conditionMap: MutableMap<ConditionType, String> = mutableMapOf(),
        var minLen: Int = 0,
        var maxLen: Int = 0,
        var minValue: Number = BigDecimal.ZERO,
        var maxValue: Number = BigDecimal.ZERO,

        var numberDataSet: Set<Number> = mutableSetOf(),
        var stringDataSet: Set<String> = mutableSetOf(),

        var startStr: Set<String> = mutableSetOf(),
        var endStr: Set<String> = mutableSetOf(),
        var containStr: Set<String> = mutableSetOf(),

        var dateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd"),
    )

    data class DelegatesValidatorBuilder(val nullErrorMsg: String) {
        private val params = DelegatesValidatorBuilderParams(nullErrorMsg = nullErrorMsg)

        fun notBlank(errorMsg: String) = apply {
            params.conditionMap[ConditionType.STRING_NOT_BLANK] = errorMsg
        }

        fun sizeRange(minLen: Int, maxLen: Int, errorMsg: String) = apply {
            params.minLen = minLen
            params.maxLen = maxLen
            params.conditionMap[ConditionType.STRING_SIZE_RANGE] = errorMsg
        }

        fun numberRang(minValue: Number, maxValue: Number, errorMsg: String) = apply {
            params.minValue = minValue
            params.maxValue = maxValue
            params.conditionMap[ConditionType.NUMBER_RANGE] = errorMsg
        }

        fun email(errorMsg: String) = apply {
            params.conditionMap[ConditionType.STRING_EMAIL] = errorMsg
        }

        fun idCard(errorMsg: String) = apply {
            params.conditionMap[ConditionType.STRING_IDCARD] = errorMsg
        }

        fun mobilePhone(errorMsg: String) = apply {
            params.conditionMap[ConditionType.STRING_MOBILE_PHONE] = errorMsg
        }

        fun stringStart(startStr: Set<String>, errorMsg: String) = apply {
            params.startStr = startStr
            params.conditionMap[ConditionType.STRING_START] = errorMsg
        }

        fun stringEnd(endStr: Set<String>, errorMsg: String) = apply {
            params.endStr = endStr
            params.conditionMap[ConditionType.STRING_END] = errorMsg
        }


        fun stringContains(containStr: Set<String>, errorMsg: String) = apply {
            params.containStr = containStr
            params.conditionMap[ConditionType.STRING_CONTAINS] = errorMsg
        }

        /**
         * 判断数据是否在指定集合中
         */
        fun numberInSet(dataSet: Set<Number>, errorMsg: String) = apply {
            params.numberDataSet = dataSet
            params.conditionMap[ConditionType.NUMBER_INSET] = errorMsg
        }

        /**
         * 判断数据是否在指定集合中
         */
        fun stringInSet(dataSet: Set<String>, errorMsg: String) = apply {
            params.stringDataSet = dataSet
            params.conditionMap[ConditionType.STRING_INSET] = errorMsg
        }

        fun date(dateFormat: DateFormat, errorMsg: String) = apply {
            params.dateFormat = dateFormat
            params.conditionMap[ConditionType.STRING_DATE] = errorMsg
        }

        fun xss(errorMsg: String) = apply {
            params.conditionMap[ConditionType.STRING_XSS] = errorMsg
        }

        fun <T : Any> build() =
            ZlcdDelegatesNotNullVar<T>(
                params
            )

    }

}

class AppException(msg: String) : RuntimeException(msg)




